package com.hzncc.flowable_diboot.contract.service.impl;

import com.diboot.core.service.impl.BaseServiceImpl;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.vo.JsonResult;
import com.hzncc.flowable_diboot.contract.entity.ContractProductMaterial;
import com.hzncc.flowable_diboot.contract.service.ContractProductMaterialService;
import com.hzncc.flowable_diboot.excel.model.ContractProductMaterialImportModel;
import com.hzncc.flowable_diboot.mapper.ContractProductMaterialMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 合同产品物料技术清单 相关Service实现类
 *
 * @author MyName
 * @version 1.0
 * @date 2024-07-12
 * Copyright © MyCorp
 */
@Slf4j
@Service
public class ContractProductMaterialServiceImpl extends BaseServiceImpl<ContractProductMaterialMapper, ContractProductMaterial> implements ContractProductMaterialService {

    @Resource
    private ContractProductMaterialMapper contractProductMaterialMapper;

    @Override
    public void deleteByContractCodeAndContractVersion(String contractCode, Long contractVersion) {
        contractProductMaterialMapper.deleteByContractCodeAndContractVersion(contractCode, contractVersion);
    }


    @Override
    public JsonResult checkImportData(Map<String, Object> data, String productId) {
        List<ContractProductMaterialImportModel> dataList = (List<ContractProductMaterialImportModel>) data.get("dataList");
        Map<String, ContractProductMaterial> importModelMap = new HashMap<>();
        Set<String> repeatSerialNumber = new HashSet<>();
        for (ContractProductMaterialImportModel importModel : dataList) {
            if (importModelMap.get(importModel.getRowNum()) != null) {
                repeatSerialNumber.add(importModel.getRowNum());
            }
            importModelMap.put(importModel.getRowNum(), BeanUtils.convert(importModel, ContractProductMaterial.class));
        }
        if (repeatSerialNumber.size() > 0){
            return JsonResult.FAIL_OPERATION("序号【"+String.join(",",repeatSerialNumber)+"】重复，请检查");
        }
        List<ContractProductMaterial> list = lambdaQuery().eq(ContractProductMaterial::getContractProductId, productId).list();
        Map<String,ContractProductMaterial> materialMap = list.stream().collect(Collectors.toMap(ContractProductMaterial::getRowNum, contractProductMaterial -> contractProductMaterial));
        for (Map.Entry<String, ContractProductMaterial> entry : importModelMap.entrySet()) {
            ContractProductMaterial mrl = materialMap.get(entry.getKey()) == null ? new ContractProductMaterial() : materialMap.get(entry.getKey());
            // 如果物料编码相同，则带出物料id
            if (Objects.equals(mrl.getMaterialCode(), entry.getValue().getMaterialCode())) {
                ContractProductMaterial material = entry.getValue();
                material.setId(mrl.getId());
                material.setContractProductId(productId);
            } else if (mrl.getMaterialCode() != null) {
                return JsonResult.FAIL_OPERATION("序号【"+entry.getKey()+"】物料编码【"+entry.getValue().getMaterialCode()+"】与原有数据【"+mrl.getMaterialCode()+"】不一致，请检查");
            }
        }
        data.put("dataList",importModelMap.values());
        return JsonResult.OK(data);
    }
}